/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yaps.petstore.dao.impl;

import com.yaps.petstore.dao.CategoryDAO;
import com.yaps.petstore.entity.catalog.Category;
import com.yaps.petstore.exception.ValidationException;
import com.yaps.petstore.util.Constants;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author pfa
 */
//This will make easier to autowired


@Repository("CategoryDAO")
public class CategoryDAOImpl implements CategoryDAO {
@PersistenceContext
private EntityManager em;

    private final String cname = this.getClass().getName();
    private Logger logger = Logger.getLogger(Constants.LOGGER_STATELESS);

    // ======================================
    // = Methodes publiques pour categorie  =
    // ======================================
    @Override
    @Transactional
    public Category createCategory(final Category category) {
        final String mname = "getNewCategory";
        logger.entering(cname, mname, category);

        // On s'assure de la validité des paramètres
        if (category == null)
            throw new ValidationException("Category object is null");

        // L'objet est persisté en base de données
        em.persist(category);
  

        logger.exiting(cname, mname, category);
        return category;
    }

    @Override
    public Category findCategory(final Long categoryId) {
        final String mname = "findCategory";
        logger.entering(cname, mname, categoryId);

        // On s'assure de la validité des paramètres
//        if (categoryId == null)
//            throw new ValidationException("Invalid id");

        Category category;

        // On recherche l'objet à partir de son identifiant
        category = em.find(Category.class, categoryId);

        logger.exiting(cname, mname, category);
        return category;
    }

    @Override
    @Transactional
    public void deleteCategory(final Category category) {
        final String mname = "deleteCategory";
        logger.entering(cname, mname, category);

        // On s'assure de la validité des paramètres
//        if (category == null)
//            throw new ValidationException("Category object is null");

        // On supprime l'objet de la base de données
           em.remove(em.merge(category));

        logger.exiting(cname, mname);
    }

    @Override
    @Transactional
    public Category updateCategory(final Category category) {
        final String mname = "updateCategory";
        logger.entering(cname, mname, category);

        // On s'assure de la validité des paramètres
//        if (category == null)
//            throw new ValidationException("Category object is null");

        // On modifie l'objet de la base de données
        em.merge(category);

        logger.exiting(cname, mname, category);
        return category;
    }

    @Override
    public List<Category> findCategories() {
        final String mname = "findCategories";
        logger.entering(cname, mname);

        Query query;
        List<Category> categories;

        // On modifie l'objet de la base de données
        query = em.createQuery("SELECT c FROM Category c ORDER BY c.name");
       
categories = query.getResultList();
        logger.exiting(cname, mname, categories.size());
        return categories;
    }
  
}
